<?php
namespace app\article;
use think\template\TagLib;
class Tag extends TagLib {
    
    protected $tags   =  [
        'articlecategory'      => ['close' => 1], 
        'articlecategoryinfo'      => ['close' => 0], 
        'article'      => ['close' => 1],
        'articleinfo'      => ['close' => 0],
        'articlefield'      => ['close' => 1],
     	'articlecomments'      => ['close' => 1],
        'articlecrumb' => ['close' => 0],
        'articlepage' => ['close' => 1],
        'articlepagelist' => ['close' => 0],
        'articledescription' => ['close' => 0],
    ];
    
    public function tagarticlecategory($tag, $content)
    {
        if (!isset($tag['id'])) {
            $tag['id'] = 'val';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                }
            }
        }
        $html  = "<?php ";
        $html .= '$__think__tag__ = \'' . json_encode($tag) . '\';';
        $html .= '$__think__list__ = "";';
        $html .= '$__think__list__ = model("app.article.model.ArticlesCategoryTpl")->getCategoryByTag($__think__tag__);';
        $html .= ' ?>';
        $html .= '{volist name="__think__list__" id="' . $tag['id'] . '" key="' . $tag['key'] . '"}';
        $html .= $content;
        $html .= '{/volist}';
        return $html;
    }
    
    public function tagarticlecategoryinfo($tag, $content)
    {
        if (!isset($tag['id'])) {
            $tag['id'] = 'val';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                }
            }
        }
        $html  = "<?php ";
        $html .= '$__think__tag__ = \'' . json_encode($tag) . '\';';
        $html .= '$__think__list__ = "";';
        $html .= '$__think__list__ = model("app.article.model.ArticlesCategoryTpl")->getCategoryByTag($__think__tag__);';
        $html .= '$'. $tag['id'] .' = $__think__list__; ?>';
        return $html;
    }
    
    public function tagarticle($tag, $content)
    {
        if (!isset($tag['id'])) {
            $tag['id'] = 'v';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        if (isset($tag['page'])) {
            $page = $tag['page'];
        } else {
            $page = '';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                    
                }
            }
        }
        $html  = '<?php ';
        $html .= '$__think__tag__ = \''.json_encode($tag).'\';';
        $html .= '$__think__list__ = "";';
        $html .= '$__think__list__ = model("app.article.model.Articles")->getItemListByTag($__think__tag__,"list");';
        if ($page) {
            $html .= '$pagination = model("app.article.model.Articles")->getItemListByTag($__think__tag__,"pagination");';
        } else {
            $html .= '$pagination = "";';
        }
        $html .= ' ?>';
        $html .= '{volist name="__think__list__" id="' . $tag['id'] . '" key="' . $tag['key'] . '"}';
        $html .= $content;
        $html .= '{/volist}';
        return $html;
    }
    

    public function tagarticlefield($tag, $content)
    {
        if (!isset($tag['id'])) {
            $tag['id'] = 'v';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        if (isset($tag['page'])) {
            $page = $tag['page'];
        } else {
            $page = '';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                    
                }
            }
        }
        $html  = '<?php ';
        $html .= '$__think__tag__ = \''.json_encode($tag).'\';';
        $html .= '$__think__list__model__ = "";';
        $html .= '$__think__list__model__ = model("app.article.model.ArticlesField")->getItemListByTag($__think__tag__,"list");';
        $html .= ' ?>';
        $html .= '{volist name="__think__list__model__" id="' . $tag['id'] . '" key="' . $tag['key'] . '"}';
        $html .= $content;
        $html .= '{/volist}';
        return $html;
    }

    public function tagarticlecomments($tag, $content)
    {
        if (!isset($tag['id'])) {
            $tag['id'] = 'v';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        if (isset($tag['page'])) {
            $page = $tag['page'];
        } else {
            $page = '';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                    
                }
            }
        }
        $html  = '<?php ';
        $html .= '$__think__tag__ = \''.json_encode($tag).'\';';
        $html .= '$__think__info__ = "";';
        $html .= '$__think__info__ = model("app.article.model.ArticlesComments")->getItemListByTag($__think__tag__,"list");';
        if ($page) {
            $html .= '$pagination = model("app.article.model.ArticlesComments")->getItemListByTag($__think__tag__,"pagination");';
        } else {
            $html .= '$pagination = "";';
        }
        $html .= ' ?>';
        $html .= '{volist name="__think__info__" id="' . $tag['id'] . '" key="' . $tag['key'] . '"}';
        $html .= $content;
        $html .= '{/volist}';
        return $html;
    }
    
 
    public function tagarticleinfo($tag, $content)
    {
        if (!isset($tag['id'])) {
            $tag['id'] = 'v';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        if (isset($tag['page'])) {
            $page = $tag['page'];
        } else {
            $page = '';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                    
                }
            }
        }
        $html  = '<?php ';
        $html .= '$__think__tag__ = \''.json_encode($tag).'\';';
        $html .= '$__think__list__ = "";';
        $html .= '$__think__list__ = model("app.article.model.Articles")->getItemListByTag($__think__tag__,"list");';
        $html .= '$'. $tag['id'] .' = $__think__list__; ?>';

        return $html;
    }
    
    public function tagarticlecrumb($tag, $content)
    {
    	if (!isset($tag['id'])) {
            $tag['id'] = 'v';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                }
            }
        }
        $html  = '<?php ';
        $html .= '$__think__tag__ = \''.json_encode($tag).'\';';
        $html .= '$__think__article__ = "";';
        $html .= '$__think__article__ = model("app.article.model.Articles")->getCrumb($__think__tag__);';
        $html .= 'echo $__think__article__; ?>';
        return $html;
    }
    
    public function tagarticlepage($tag, $content)
    {
        if (!isset($tag['id'])) {
            $tag['id'] = 'v';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                    
                }
            }
        }
        $html  = '<?php ';
        $html .= '$__think__tag__ = \''.json_encode($tag).'\';';
        $html .= '$__think__list__ = "";';
        $html .= '$__think__list__ = model("app.article.model.Articles")->getPage($__think__tag__);';
        $html .= ' ?>';
        $html .= '{volist name="__think__list__" id="' . $tag['id'] . '" key="' . $tag['key'] . '"}';
        $html .= $content;
        $html .= '{/volist}';
        return $html;
    }
    
    public function tagarticlepagelist($tag, $content)
    {
    	if (!isset($tag['id'])) {
            $tag['id'] = 'v';
        }
        if (!isset($tag['key'])) {
            $tag['key'] = 'index';
        }
        if (isset($tag['page'])) {
            $page = $tag['page'];
        } else {
            $page = '';
        }
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                }
            }
        }
        $html  = '<?php ';
        $html .= '$__think__tag__ = \''.json_encode($tag).'\';';
        $html .= '$__think__article__ = "";';
     	$html .= '$__think__article__ = model("app.article.model.Articles")->getItemListByTag($__think__tag__,"pagination");';
        $html .= 'echo $__think__article__; ?>';
        return $html;
    }
    
    public function tagarticledescription($tag, $content)
    {
        foreach ($tag as $key => $val) {
            if (strpos($val, '$') !== false) {
                if (substr($val, 0, 1) == '$') {
                    if (strpos($val, '"') !== false) {
                        $val = str_replace('"', "'", $val);
                    }
                    $tag[$key] = "'.". $val . ".'";
                }
            }
        }
        $html  = '<?php ';
        $html .= '$__think__tag__ = \''.json_encode($tag).'\';';
        $html .= '$__think__description__ = "";';
        $html .= '$__think__description__ = model("app.article.model.Articles")->getDescription($__think__tag__);';
        $html .= 'echo $__think__description__; ?>';
        return $html;
    }
}
